<?php

namespace app\command;

use app\common\lib\BaseCommand;
use app\common\lib\Common;
use app\logic\cron\DouyinLogic;
use app\logic\cron\QiCatLogic;
use bootstrap\DBListen;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleEvent;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Webman\Config;
use Webman\Event\Event;


class CronQiCat extends Command
{
    protected static $defaultName = 'cron:qicat';
    protected static $defaultDescription = 'cron qicat';

    /**
     * @return void
     */
    protected function configure()
    {
        //发布执行结束
        Event::dispatch('console.start', []);

        $this->addArgument('name', InputArgument::OPTIONAL, 'Name description');

        Common::initTaskConfig();
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int
     */
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $name = $input->getArgument('name');
        $cmd = 'php webman '.self::$defaultName.($name?' '.$name:'');
        try{
            $startTime = round(microtime(true),4);

            $result = [];
            switch ($name){
                case 'getNovelList':
                    $result = $this->getNovelList();
                    break;
                case 'updateNovelByUpload':
                    $result = $this->updateNovelByUpload();
                    break;
            }

            $ttls =  round(round(microtime(true),4) - $startTime,4);
            //发布执行结束
            Event::dispatch('console.terminate', [$cmd,$result,$ttls]);

            return self::SUCCESS;
        }catch (\Exception $e){
            //var_dump($e->getMessage());
            Common::consoleExecptionReport($cmd,$e);
            return self::FAILURE;
        }catch (\Throwable $e){
            //var_dump($e->getMessage());
            Common::consoleExecptionReport($cmd,$e);
            return self::FAILURE;
        }

    }

    /**
     * 获取小说列表
     * @return array
     * @throws \app\exception\IndexException
     */
    public function getNovelList()
    {
        $result = QiCatLogic::getInstance()->getNovelList();

        return $result;
    }

    /**
     * 更新后台的导入小说
     * @return array
     * @throws \app\exception\IndexException
     */
    public function updateNovelByUpload()
    {
        $result = QiCatLogic::getInstance()->updateNovelByUpload();

        return $result;
    }


}
